<?php

class IndexController extends Zend_Controller_Action
{
	protected $_email;
	
    public function init()
    {
        $this->_email = new Application_Model_Email();
    }

    /**
     * Action redireciona para pagina de autenticacao
     */
    public function indexAction()
    {  	
      $this->_redirect('admin/');
    }
    
    /**
     * Action para descadastrar email
     * @since 27/05/2012
     */
    public function optoutAction()
    {
    	  if ($this->_hasParam('tk') == false) 
        {
            $this->_redirect('/admin/');
            
        } else {
        	$token = $this->_request->getParam('tk');
        	$result = $this->_email->findByEmailToken($token);
        	
        	if(count($result)!=0)//se token for valído
        	{
        		$form = new Application_Form_Email();
        		$form->removeElement('nome');
        		$form->removeElement('email');
        		$form->removeElement('sexo');
        		$form->removeElement('cidade');
        		$form->removeElement('datanascimento');
        		$form->removeElement('estado');
        		$form->removeElement('optin');
        		$form->removeElement('lista');
        		$form->getElement('Salvar')
        			->setName('Descadastrar')
        			->setAttrib('title', 'Descadastrar');
        		$this->view->form = $form; 
        		$this->view->email = $result['email'];       		
        		if($this->_request->isPost()){
		    		$this->_email->howOptout($result['idemail']);
		    		$this->_forward('optout-sucesso'); 
        		}
        		
        	}else{//se token for invalido        	
        		$this->_forward('optout-erro');        		
        	}
        }
    }
    public function optoutErroAction()
    {
    }
    public function optoutSucessoAction()
    {
    }

}

